package com.itcpay.jdk8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class PredicateTest2 {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        PredicateTest2 pt = new PredicateTest2();
        // 求偶数
        pt.conditionFilter(list, (i) -> i % 2 == 0);
        System.out.println("------------------------");

        // 求奇数
        pt.conditionFilter(list, i -> i % 2 != 0);
        System.out.println("------------------------");

        // 求 >5
        pt.conditionFilter(list, i -> i > 5);
        System.out.println("------------------------");

        // 求 <3
        pt.conditionFilter(list, i -> i < 3);
        System.out.println("------------------------");

        // 打印所有集合元素
        pt.conditionFilter(list, i -> true);
        System.out.println("------------------------");

        // 不打印集合中任何元素
        pt.conditionFilter(list, i -> false);
        System.out.println("------------------------");

        // > 5 and 是偶数
        pt.conditionFilter2(list, item -> item > 5, item -> item % 2 == 0);
        System.out.println("------------------------");



    }


    public void conditionFilter(List<Integer> list, Predicate<Integer> predicate) {
        for (Integer i : list) {
            if (predicate.test(i)) {
                System.out.println(i);
            }
        }
    }

    public void conditionFilter2(List<Integer> list, Predicate<Integer> predicate, Predicate<Integer> predicate2) {
        for (Integer integer:list) {
            if (predicate.and(predicate2).test(integer)) {
                System.out.println(integer);
            }
        }
    }

}
